import java.util.LinkedList;
import java.util.Queue;

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int left = 0;
        int right = 0;
        int ret = 0;
        int[] hash = new int[128];
        while(right < s.length()) {
            char chR = s.charAt(right);
            hash[chR]++;//进窗口
            while(hash[chR] > 1) {//判断
                char chL = s.charAt(left++);
                hash[chL]--;//出窗口
            }
            ret = Math.max(ret, right - left + 1);//更新结果
            right++;
        }
        return ret;
    }
    public int lengthOfLongestSubstring2(String s) {
        int left = 0;
        int right = 0;
        int ret = 0;
        Queue<Character> queue = new LinkedList<>();
        while(right < s.length()) {
            char ch = s.charAt(right);
            if(!queue.contains(ch)) {
                ret = Math.max(ret, right - left + 1);
            }else {
                while(queue.contains(ch) && left != right) {
                    queue.poll();
                    left++;
                }
            }
            queue.offer(ch);
            right++;
        }
        return ret;
    }
}